package com.hjm.app.tool;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hjm.ui.common.ImageLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片标签选择状态监控
 *
 */
public class ImageLabelSelectStateObserve implements View.OnClickListener {
    private static final String TAG = "SelectStateObserve";
    List<ViewParent> viewParentList;
    UpdateUI updateUI;

    /**
     * 图片标签选择状态监控
     * @param updateUI 更新UI回调
     */
    public ImageLabelSelectStateObserve(UpdateUI updateUI) {
        this.viewParentList = new ArrayList<>();
        this.updateUI = updateUI;
    }

    /**
     * 将视图添加到状态监视组<br>
     * 可添加的视图为：<br>
     * - <code>LinearLayout</code>：线性布局<br>
     * - <code>RelativeLayout</code>：相对布局<br>
     * - <code>FrameLayout</code>：帧布局<br>
     * - <code>ConstraintLayout</code>：约束布局<br>
     * - <code>CoordinatorLayout</code>：协调布局<br>
     * - <code>GridLayout</code>：网格布局
     * - <code>ImageLabel</code>：自定义视图
     *
     * @param layoutParent  父视图
     * @param defaultSelect 是否默认选中
     * @param <T>
     */
    public <T extends ViewGroup> void addToObserveGroup(T layoutParent, Boolean defaultSelect) {
        if (layoutParent == null) {
            return;
        }
        ViewParent viewParent = new ViewParent(layoutParent);

        LinearLayout linearLayout = (LinearLayout)layoutParent;
        if(layoutParent instanceof ImageLabel){
            linearLayout = (LinearLayout)layoutParent.getChildAt(0);
        }
        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            viewParent.subViewList.add(linearLayout.getChildAt(i));
        }


        if (defaultSelect) {
            updateSelect(true, viewParent);
        }
        viewParentList.add(viewParent);
        layoutParent.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        for (int i=0;i<viewParentList.size();i++){
            ViewParent viewParent = viewParentList.get(i);
            updateSelect(v.getId() == viewParent.id, viewParent);
        }
        updateUI.onClick(v);
    }

    private void updateSelect(Boolean selectState,ViewParent viewParent) {
        List<View> subViewList = viewParent.subViewList;
        // 之前为选中状态且当前为未选中则视图变为未选中状态
        if(viewParent.selectState && !selectState){
            viewParent.selectState = false;
            for (int i=0;i<subViewList.size();i++){
                updateUI.updateUIToNonactive(subViewList.get(i));
            }
            updateUI.updateUIToNonactiveWithExtraView(viewParent);
        }

        // 之前为未选中状态且当前为选中状态则视图变为选中状态
        if(!viewParent.selectState && selectState){
            viewParent.selectState = true;
            for (int i=0;i<subViewList.size();i++){
                updateUI.updateUIToActive(subViewList.get(i));
            }
            updateUI.updateUIToActiveWithExtraView(viewParent);
        }


    }

    public class ViewParent extends View{
        List<View> subViewList;
        public List<View> extraSubViewList;
        public Boolean selectState;
        public ViewGroup layoutParent;
        int id;

        public  <T extends ViewGroup>  ViewParent(T layoutParent) {
            super(layoutParent.getContext());
            subViewList = new ArrayList<>();
            extraSubViewList = new ArrayList<>();
            this.layoutParent = layoutParent;
            this.id = layoutParent.getId();
            if(layoutParent instanceof ImageLabel){
                this.layoutParent = (LinearLayout)layoutParent.getChildAt(0);
            }
            this.selectState = false;
        }
    }
    public interface UpdateUI{
        /**
         * 自定义更新UI为激活状态
         *
         * @param viewSub 要更新的视图
         */
        void updateUIToActive(View viewSub);

        /**
         * 自定义更新UI为非激活状态
         *
         * @param viewSub 要更新的视图
         */
        void updateUIToNonactive(View viewSub);

        /**
         * 点击事件
         * @param view
         */
        void onClick(View view);

        void updateUIToActiveWithExtraView(ViewParent viewParent);

        void updateUIToNonactiveWithExtraView(ViewParent viewParent);
    }
}
